package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;

import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TableLayout table;
    private TableRow head;
    private TableLayout left;
    private NestedScrollView svLeft;
    private NestedScrollView svTable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        table = findViewById(R.id.table);
        head = findViewById(R.id.head);
        left = findViewById(R.id.left);
        svLeft = findViewById(R.id.sv_left);
        svTable = findViewById(R.id.sv_table);

        //双向绑定滚动时间
        svTable.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                svLeft.scrollTo(scrollX, scrollY);
            }
        });
        //双向绑定滚动时间
        svLeft.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                svTable.scrollTo(scrollX, scrollY);
            }
        });

        //顶部表头
        for (int i = 0; i < 20; i++) {
            TextView textView = new TextView(this);
            textView.setWidth(200);
            textView.setPadding(10, 10, 10, 10);
            textView.setText("head:" + i);
            head.addView(textView);
        }
        //左侧表头
        for (int i = 0; i < 50; i++) {
            TextView textView = new TextView(this);
            textView.setWidth(100);
            textView.setPadding(10, 10, 10, 10);
            textView.setText("left:" + i);
            left.addView(textView);
        }

        //行数据
        for (int i = 0; i < 50; i++) {
            TableRow row = new TableRow(this);

            //列数据
            for (int j = 0; j < 20; j++) {
                TextView textView = new TextView(this);
                textView.setPadding(10, 10, 10, 10);
                textView.setWidth(200);
                textView.setText("x:" + i + ",y:" + j);
                row.addView(textView);
            }
            table.addView(row);
        }
    }
}